/**
 * jims
 */
package com.jims.master.modules.doctor.medicalService.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.NumberUtil;
import com.jims.master.modules.base.medicalService.entity.SDefaultServe;
import com.jims.master.modules.base.medicalService.service.SDefaultServeService;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.doctor.medicalService.dao.SDoctorServeDao;
import com.jims.master.modules.doctor.medicalService.entity.SDoctorServe;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 医生提供的服务表Service
 * @author xueyx
 * @version 2017-05-03
 */
@Service
@Transactional(readOnly = true)
public class SDoctorServeService extends CrudService<SDoctorServeDao, SDoctorServe> {

	@Autowired
	private SDefaultServeService sDefaultServeService;
	@Autowired
	private MasterDoctorinfoService masterDoctorinfoService;

	@Override
    public SDoctorServe get(String id) {
		return super.get(id);
	}

	@Override
    public List<SDoctorServe> findList(SDoctorServe sDoctorServe) {
		return super.findList(sDoctorServe);
	}

	@Override
    public Page<SDoctorServe> findPage(Page<SDoctorServe> page, SDoctorServe sDoctorServe) {
		return super.findPage(page, sDoctorServe);
	}

	@Override
    @Transactional(readOnly = false)
	public int save(SDoctorServe sDoctorServe) {
		return super.save(sDoctorServe);
	}

	@Override
    @Transactional(readOnly = false)
	public int delete(SDoctorServe sDoctorServe) {
		return super.delete(sDoctorServe);
	}

	@Transactional(readOnly = false)
	public void saveAll(SDoctorServe sDoctorServe) {
		for (SDefaultServe s : sDoctorServe.getServes()) {
			if (StringUtils.isNotEmpty(s.getId())) {
				SDoctorServe entity = new SDoctorServe();
				entity.setAreaDetailed(sDoctorServe.getAreaDetailed());
				entity.setDefaultId(s.getId());
				entity.setOrderNo(sDoctorServe.getOrderNo());
				entity.setServeType(sDoctorServe.getServeType());
				entity.setServeState(sDoctorServe.getServeState());
				entity.setTimeType(sDoctorServe.getTimeType());
				entity.setDiscount(sDoctorServe.getDiscount());
				SDefaultServe sDefaultServe = sDefaultServeService.get(s.getId());
				if (sDefaultServe.getUseFlag().equals("0")) {
					sDefaultServe.setUseFlag("1");
					sDefaultServeService.save(sDefaultServe);
				}
				MasterDoctorinfo masterDoctorinfo = masterDoctorinfoService.getDocByBaseinfo(new MasterBaseinfo(UserUtils.getUser().getPersionId()));
				entity.setDoctorId(masterDoctorinfo.getId());
				entity.setTitle(sDefaultServe.getTitle());
				entity.setIntroduce(sDefaultServe.getIntroduce());
				entity.setPicUrl(sDefaultServe.getPicUrl());
				entity.setServeContent(sDefaultServe.getServeContent());
				entity.setPrice(sDefaultServe.getPrice());
				Double cost = NumberUtil.mul(Double.valueOf(entity.getDiscount()), entity.getPrice(), 2);
				entity.setCost(cost);
				super.save(entity);
			}
		}
	}

	/**
	 * 根据系统服务名称查询签约医生
	 *
	 * @return
	 * @author chentao
	 */
	public List<SDoctorServe> sDoctorServeList(String title) {
		return dao.sDoctorServeList(title);
	}

	/**
	 * 根据医生ID得到签约服务
	 *
	 * @param
	 * @return
	 */
	public List<SDoctorServe> serveListByDoctor(String doctorId) {
		return dao.serveListByDoctor(doctorId);
	}

	public SDoctorServe getByEntity(SDoctorServe sDoctorServe) {
		return dao.getByEntity(sDoctorServe);
	}

	/**
	 * @Title:
	 * @Description: （获取服务列表）
	 * @param
	 * @return
	 * @author jict
	 * @date 2017/12/8
	 * @throws
	 */
	public List<SDoctorServe> doctorServerList() {
		return dao.doctorServerList();
	}

	public List<SDoctorServe> doctorList(String area,String titles,String name, String hospital,String depNo,List<String> list,String type,int pageNum,String parts) {
		return dao.doctorList(area,titles,name,hospital,depNo,list,type,pageNum,parts);
	}

	public List<SDoctorServe> doctorListByArea(String hospital,String depNo,List<String> list,String type,String area) {
		return dao.doctorListByArea(hospital,depNo,list,type,area);
	}

	public List<SDoctorServe> doctorListByAll(String hospital,String depNo,List<String> list,String type) {
		return dao.doctorListByAll(hospital,depNo,list,type);
	}

	public SDoctorServe getServerById(String serverId) {
		return dao.getServerById(serverId);
	}

	public SDoctorServe getServerByIds(String serverId) {
		return dao.getServerByIds(serverId);
	}

	/**
	 * @Title:
	 * @Description: （医生停运服务）
	 * @param
	 * @return
	 * @author jict
	 * @date 2017/12/8
	 * @throws
	 */
	@Transactional(readOnly = false)
	public int stopServer(String serverId,String state) {
		return dao.stopServer(serverId,state);
	}
	@Transactional(readOnly = false)
	public int setCycle(String serverId,String cycle,String cost) {
		return dao.setCycle(serverId,cycle,cost);
	}
}